<?php
namespace app\admin\controller;

use app\common;
use think\Controller;
use think\Session;
use think\Request;
use think\File;
class Setting extends common
{
    //列表
    public function index(){
//        var_dump($this->login());exit;
        //获取登录者信息
        $admin=$this->login();
        $sql=db('operator')->where("name",$admin)->find();
		if($sql['is_admin']==1)
		{
            return $this->manager();
		}
		else
		{
            $sql=db('operator')->where("name",$admin)->find();
            $this->assign('list',$sql);
            $this->assign("loginname",$this->login());
            return  $this->fetch("setting/index");
		}

	}


    public function manager()
    {
        $where = '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $id = (isset($_GET['id'])) ? trim($_GET['id']) : '';
        //搜索条件

        if (strlen($id)) {
            $where = "id='{$id}'";
        }
        if ($keyword){
            $where = "name LIKE '%{$keyword}%'";
        }

        //查询总数据
        $maxpages = Db('operator')->where($where)->count('*');
        //最大页数
        $maxpage = $maxpages / 10;
        // 查询状态为0的数据 并且每页显示10条数据 总记录数为1000
        $list = db('operator')->where($where)->paginate(20, $maxpages);
//        return $this->sucReturn($list);exit;
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign('id', $id);
        $this->assign('keyword', $keyword);
        $this->assign('list', $list);
        $this->assign('loginname', $this->login());
        $this->assign('page', $page);
        $this->assign('maxpage', $maxpage);
        $this->assign('maxpages', $maxpages);
        // 渲染模板输出
//        echo"1111";exit;
        return $this->fetch("setting/manager");
    }




    //添加管理员
    public function add(){
        $this->assign("loginname",$this->login());
        return $this->fetch("setting/manager_add");
    }


    //添加新管理员数据
    public function insert(){
        //添加新管理员的超级管理员用户名(备用)
        $admin=$this->login();
        if(!empty($_POST['name']) && !empty($_POST['password']) && !empty($_POST['email'])) {
            $salt = rand(0, 999999);
            $password = md5(md5($_POST['password']) . $salt);
            $data=[
                'name'    => $_POST['name'],
                'password'=> $password,
                'salt'    => $salt,
                'email'   => $_POST['email'],
                'status'  => 1,
                'logins'  => 0,
                'is_admin'=> 0,
                'last_ip' =>  "还没登录过",
                'add_time'=>time(),
            ];
            $new_id=db("operator")->insertGetId($data);
            if($new_id){
                $this->success("添加新管理员成功","setting/manager");
            }else{
                $this->error("添加失败");
            }
        }else{
            $this->error("帐号、密码、邮箱不能为空");
        }

    }


    //加载修改页面
    public function edit_manager($id){
        $info = db("operator")->where("id",$id)->find();
        $this->assign("info",$info);
        $this->assign("loginname",$this->login());
        $this->assign("msg",0);
        return $this->fetch("setting/edit_manager");
    }

    //修改数据
    public function update(){
        $id=$_POST['id'];
        $password =$_POST['password'];
        $sql = db("operator")->where("id",$id)->find();
        //判断登录的是否是超级管理员
        $admin = $this->login();
        $is_admin = db("operator")->where('name',$admin)->find();
        $adminid=$is_admin['is_admin'];
        $oldpassword =$sql['password'];
        if($password == $oldpassword){
            $password = $oldpassword;
            $salt = $sql['salt'];
        }else{
            $salt=rand(0,999999);
            $password = md5(md5($password).$salt);

        }
        $data=[
            'name'  =>$_POST['name'],
            'password' => $password,
            'salt'     => $salt,
            'email'    => $_POST['email'],
        ];
//        var_dump($data);exit
        $update = db("operator")->where("id",$id)->update($data);
        if($update){
            if($adminid == 1){
                $this->success("修改成功","setting/manager");
            }else{
                $this->success("修改成功","setting/index");
            }
        }else{
                $this->error("修改失败");
        }
    }
    //删除数据
    public function delete($id)
    {
        $list = db("operator")->find($id);
        if ($list > 0) {
            $sql = db('operator')->where("id", $id)->delete();
            if ($sql) {
                $this->success("删除成功", "setting/index");
            } else {
                $this->error("删除失败");
            }
        }
    }
    //加载修改密码页
    public function password(){
//        $admin=$this->login();
//        $list=db("operator")->where("name",$admin)->find();
//        $id=$list['id'];
        $this->assign("loginname",$this->login());
        $this->assign("msg",0);
        return $this->fetch("setting/password");
    }

    //修改密码
	public function edit_pass()
    {
        $admin=$this->login();
        $list=db("operator")->where("name",$admin)->find();
//        echo $list['id'];exit;
		if ($_POST) {
			$new_password = isset($_POST['new_password']) ? $_POST['new_password'] : '';
			$confirm_password = isset($_POST['confirm_password']) ? $_POST['confirm_password'] : '';
			if ($new_password && $new_password == $confirm_password) {
				$salt =rand(0,999999);
                $new_password = md5(md5($new_password).$salt);

//				$sql = "UPDATE w_operator SET password='{$new_password}', salt='{$salt}' WHERE id='{$this->admin_id}' ";
                $data=[
                    'password' => $new_password,
                    'salt'     => $salt,
                ];
                $sql = db("operator")->where("id",$list['id'])->update($data);
                if($sql){
                    $this->assign("loginname",$this->login());
                    $this->assign("msg",1);
                    return $this->fetch("setting/password");
                }
			} else {
                $this->assign("loginname",$this->login());
                $this->assign("msg",2);
                return $this->fetch("setting/password");
			}
		} else {
			$msg = isset($_GET['msg']) ? 1 : 0;
			$this->assign("msg", $msg);
            return $this->fetch("setting/password");
		}
	}

}






























//class setting extends core {
//	public function __construct() {
//		parent::__construct();
//	}
//
//	//index
//	public function index() {
//		if($_SESSION['admin']->is_admin==1)
//		{
//			$this->manager();
//		}
//		else
//		{
//            $sql = "SELECT * FROM w_operator WHERE id='{$this->admin_id}'";
//            $rs = $this->db->get_row($sql);
//            $this->smarty->assign('rs',$rs);
//            $this->smarty->assign('is_admin', $_SESSION['admin']->is_admin);
//            $this->display();
//		}
//
//	}
//
//	//修改密码
//	public function password() {
//		if (IS_POST) {
//			$new_password = isset($_POST['new_password']) ? $_POST['new_password'] : '';
//			$confirm_password = isset($_POST['confirm_password']) ? $_POST['confirm_password'] : '';
//			if ($new_password && $new_password == $confirm_password) {
//				$salt = generate_salt();
//				$new_password = crypt_password($new_password, $salt);
//				$sql = "UPDATE w_operator SET password='{$new_password}', salt='{$salt}' WHERE id='{$this->admin_id}' ";
//				$this->db->query($sql);
//				header("Location: ?app=setting&act=password&msg=1");
//			} else {
//				error("两次输入的密码不一致");
//			}
//		} else {
//			$msg = isset($_GET['msg']) ? 1 : 0;
//			$this->smarty->assign("msg", $msg);
//			$this->display('password.html');
//		}
//	}
//
//	//管理员列表
//	public function manager() {
//		$where = '';
//		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
//		$id = (isset($_GET['id'])) ? trim($_GET['id']) : '';
//
//		if ($keyword) {
//			$where .= "AND (name LIKE '%{$keyword}%')";
//		}
//		if (strlen($id)) {
//			$where .= "AND id='{$id}' ";
//		}
//		//分页
//		$this->setpages(array('id'=>$id, 'keyword'=>$keyword));
//
//		$sql = "SELECT *
//			FROM w_operator
//			WHERE 1 {$where} ORDER BY id ASC";
//		// echo $sql;
//		$rs = $this->ezr->get_results($sql);
//		$nav = $this->ezr->get_navigation();
//		$this->smarty->assign('rs',$rs);
//		$this->smarty->assign('keyword',$keyword);
//		$this->smarty->assign('id',$id);
//		$this->smarty->assign('page',$nav);
//
//		$this->smarty->assign('is_admin', $_SESSION['admin']->is_admin);
//		$this->display('manager.html');
//	}
//
//	//添加管理员
//	public function manager_add() {
//		//var_dump($_POST);exit();
//		if (IS_POST) {
//			$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
//			$is_admin = isset($_POST['is_admin']) ? intval($_POST['is_admin']) : 0;
//			$name = isset($_POST['name']) ? trim($_POST['name']) : '';
//
//			$password = isset($_POST['password']) ? $_POST['password'] : '';
//
//            $email =  isset($_POST['email']) ? trim($_POST['email']) : '';
//
//			$permissions = isset($_POST['permissions']) ? $_POST['permissions'] : '';
//			if ($id>0) {
//				$set = '';
//				if ($password) {
//					$salt = generate_salt();
//					$password = crypt_password($password, $salt);
//					$set = ", password='{$password}', salt='{$salt}' ";
//				}
//				$sql = "UPDATE w_operator SET email='{$email}', name='{$name}', {$set} WHERE id='{$id}'";
//				$this->db->query($sql);
//				$url = "?app=setting&act=manager_add&id={$id}&msg=1";
//			} else {
//				$salt = generate_salt();
//				$password = crypt_password($password, $salt);
//				$sql = "INSERT INTO w_operator(name, password, salt, is_admin, add_time,email)
//					VALUES('{$name}',  '{$password}', '{$salt}', '{$is_admin}', '".time()."','{$email}')";
//				$iiv = $this->db->query($sql);
//				//var_dump($iiv);exit();
//                $this->db->insert_id;
//				$url = "?app=setting&act=manager";
//			}
//            //处理权限
//            $per="";
//            for($i=0;$i<8;$i++)
//            {
//                $j=0;
//                for($j;$j<count($permissions);$j++)
//                {
//                    if($permissions[$j]==$i)
//                    {
//                        $per.='1|';
//                        break;
//                    }
//                }
//                if($j==count($permissions))
//                {
//                    $per.='0|';
//                }
//            }
//            $per=substr($per,0,strlen($per)-1);
//            $sql="UPDATE w_operator SET permission = '{$per}' WHERE id = '{$id}'";
//            $this->db->query($sql);
//			header("Location: {$url}");
//		} else {
//			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
//			if ($id) {
//				$sql = "SELECT * FROM w_operator WHERE id='{$id}'";
//				$row = $this->db->get_row($sql);
//				$row = array("id"=>$row->id, "name"=>$row->name,
//                    "is_admin"=>$row->is_admin,"email"=>$row->email,"permission"=>$row->permission);
//                if($row['permission']=='')
//                {
//                    $row['permission']="0|0|0|0|0|0|0";
//                }
//			} else {
//				$row = array("id"=>0, "name"=>"", "is_admin"=>0,"email"=>"","permission"=>"0|0|0|0|0|0|0");
//			}
//			$current_permissions = explode('|',$row['permission']);
//			$msg = isset($_GET['msg']) ? 1 : 0;
//			$this->smarty->assign("msg", $msg);
//			$this->smarty->assign('row',$row);
//            $this->smarty->assign('permission',$current_permissions);
//            $this->smarty->assign('is_admin', $_SESSION['admin']->is_admin);
//			$this->display();
//		}
//	}
//
//	//删除管理员
//	public function manager_delete() {
//		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
//		$sql = "DELETE FROM w_operator WHERE id='{$id}'";
//		$this->db->query($sql);
//		header("Location:?app=setting&act=manager");
//	}
//
//	//日志列表
//	public function log() {
//		$where = '';
//		$member_name = (isset($_GET['member_name']) && trim($_GET['member_name'])) ? trim($_GET['member_name']) : '';
//		$log_app = (isset($_GET['log_app']) && trim($_GET['log_app'])) ? trim($_GET['log_app']) : '';
//		$log_act = (isset($_GET['log_act']) && trim($_GET['log_act'])) ? trim($_GET['log_act']) : '';
//		$ip = (isset($_GET['ip']) && trim($_GET['ip'])) ? trim($_GET['ip']) : '';
//		$id = (isset($_GET['id'])) ? trim($_GET['id']) : '';
//
//		if ($member_name) {
//			$where .= "AND member_name='{$member_name}' ";
//		}
//		if ($log_app) {
//			$where .= "AND app='{$log_app}' ";
//		}
//		if ($log_act) {
//			$where .= "AND act='{$log_act}' ";
//		}
//		if ($ip) {
//			$where .= "AND ip='{$ip}' ";
//		}
//		if (strlen($id)) {
//			$where .= "AND id='{$id}' ";
//		}
//		//分页
//		$this->setpages(array('id'=>$id, 'member_name'=>$member_name, 'log_app'=>$log_app, 'log_act'=>$log_act, 'ip'=>$ip));
//		$this->ezr->num_results_per_page=20;
//		$sql = "SELECT *
//			FROM w_log
//			WHERE 1 {$where} ORDER BY id DESC";
//		// echo $sql;
//		$rs = $this->ezr->get_results($sql);
//		$nav = $this->ezr->get_navigation();
//		$this->smarty->assign('rs',$rs);
//		$this->smarty->assign('member_name',$member_name);
//		$this->smarty->assign('log_app',$log_app);
//		$this->smarty->assign('log_act',$log_act);
//		$this->smarty->assign('ip',$ip);
//		$this->smarty->assign('id',$id);
//		$this->smarty->assign('page',$nav);
//
//		$this->display('log.html');
//	}
//
//}